package com.csii.modules.online.cgform.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import com.csii.core.api.Result;
import com.csii.core.constant.CommonConstant;
import com.csii.core.system.query.QueryGenerator;
import com.csii.modules.online.cgform.entity.OnlCgformIndex;
import com.csii.modules.online.cgform.service.IOnlCgformIndexService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController("onlCgformIndexController")
@RequestMapping({"/online/cgform/index"})
public class CgformIndexController {
   private static final Logger log = LoggerFactory.getLogger(CgformIndexController.class);
   @Autowired
   private IOnlCgformIndexService onlCgformIndexService;

   /**
    * 根据HeadId查询
    * @param headId
    * @return Result
    */
   @GetMapping({"/listByHeadId"})
   public Result<?> listByHeadId(@RequestParam("headId") String headId) {
      QueryWrapper queryWrapper = new QueryWrapper();
      queryWrapper.eq("cgform_head_id", headId);
      queryWrapper.eq("del_flag", CommonConstant.DEL_FLAG_0);
      queryWrapper.orderByDesc("create_time");
      List list = this.onlCgformIndexService.list(queryWrapper);
      return Result.ok(list);
   }

   /**
    *
    * @param cgformIndex
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @GetMapping({"/list"})
   public Result<IPage<OnlCgformIndex>> list(OnlCgformIndex cgformIndex, @RequestParam(name = "pageNo",defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize, HttpServletRequest req) {
      Result result = new Result();
      QueryWrapper wrapper = QueryGenerator.initQueryWrapper(cgformIndex, req.getParameterMap());
      Page page = new Page((long)pageNo, (long)pageSize);
      IPage iPage = this.onlCgformIndexService.page(page, wrapper);
      result.setSuccess(true);
      result.setResult(iPage);
      return result;
   }

   /**
    * 添加操作

    * @param cgformIndex
    * @return
    */
   @PostMapping({"/add"})
   public Result<OnlCgformIndex> add(@RequestBody OnlCgformIndex cgformIndex) {
      Result result = new Result();

      try {
         this.onlCgformIndexService.save(cgformIndex);
         result.success("添加成功！");
      } catch (Exception e) {
         log.error(e.getMessage(), e);
         result.error500("操作失败");
      }

      return result;
   }

   /**
    * 编辑操作
    *
    * @param cgformIndex
    * @return
    */
   @PutMapping({"/edit"})
   public Result<OnlCgformIndex> edit(@RequestBody OnlCgformIndex cgformIndex) {
      Result result = new Result();
      OnlCgformIndex one = (OnlCgformIndex)this.onlCgformIndexService.getById(cgformIndex.getId());
      if (one == null) {
         result.error500("未找到对应实体");
      } else {
         boolean su = this.onlCgformIndexService.updateById(cgformIndex);
         if (su) {
            result.success("修改成功!");
         }
      }

      return result;
   }

   /**
    * 根据id删除
    * @param id
    * @return Result
    */
   @DeleteMapping({"/delete"})
   public Result<OnlCgformIndex> delete(@RequestParam(name = "id",required = true) String id) {
      Result result = new Result();
      OnlCgformIndex one = (OnlCgformIndex)this.onlCgformIndexService.getById(id);
      if (one == null) {
         result.error500("未找到对应实体");
      } else {
         boolean su = this.onlCgformIndexService.removeById(id);
         if (su) {
            result.success("删除成功!");
         }
      }

      return result;
   }

   /**
    * 批量删除
    * @param ids
    * @return
    */
   @DeleteMapping({"/deleteBatch"})
   public Result<OnlCgformIndex> deleteBatch(@RequestParam(name = "ids",required = true) String ids) {
      Result result = new Result();
      if (ids != null && !"".equals(ids.trim())) {
         this.onlCgformIndexService.removeByIds(Arrays.asList(ids.split(",")));
         result.success("删除成功!");
      } else {
         result.error500("参数不识别！");
      }

      return result;
   }

   /**
    * id查询
    * @param id
    * @return Result
    */
   @GetMapping({"/queryById"})
   public Result<OnlCgformIndex> queryById(@RequestParam(name = "id",required = true) String id) {
      Result result = new Result();
      OnlCgformIndex one = (OnlCgformIndex)this.onlCgformIndexService.getById(id);
      if (one == null) {
         result.error500("未找到对应实体");
      } else {
         result.setResult(one);
         result.setSuccess(true);
      }

      return result;
   }
}
